package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.BoxBean;
import cn.innosmart.aq.util.GatewayListUtil;
import cn.innosmart.aq.util.SharedUtil;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Box列表界面的适配器
 * Created by David on 2015/7/7.
 */
public class BoxListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;
    private ArrayList<BoxBean> boxBeanList = new ArrayList<>();
    private BoxBean boxBean;
    private View.OnClickListener mOnClickListener;
    private boolean isModify = false;

    public BoxListAdapter(Context context) {
        this.context = context;
        if (SystemConstant.boxBeanList == null) {
            String gatewayInfo = SharedUtil.getInstance().readGatewayInfo();
            SystemConstant.boxBeanList = GatewayListUtil.getInstance().analyseGatewayInfo(gatewayInfo.toString());
            boxBeanList = SystemConstant.boxBeanList;
        } else {
            boxBeanList = SystemConstant.boxBeanList;
        }
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return boxBeanList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return boxBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_boxlist_item, null);
            holder.listview_boxlist_linecolor = convertView.findViewById(R.id.listview_boxlist_linecolor);
            holder.listview_boxlist_boxtitle = (TextView) convertView.findViewById(R.id.listview_boxlist_boxtitle);
            holder.listview_boxlist_arrow = (ImageView) convertView.findViewById(R.id.listview_boxlist_arrow);
            holder.listview_edit = (TextView) convertView.findViewById(R.id.edit_text);
            holder.listview_connect = (TextView) convertView.findViewById(R.id.connect_text);
            holder.rl_boxlayout = (RelativeLayout) convertView.findViewById(R.id.rl_boxlayout);
            holder.rl_addlayout = (RelativeLayout) convertView.findViewById(R.id.rl_addlayout);
            convertView.setTag(holder);

            //for autolayout in listview
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == boxBeanList.size()) {
            holder.rl_boxlayout.setVisibility(View.GONE);
            if(SystemConstant.loginStatus == SystemConstant.Online) {
                holder.rl_addlayout.setVisibility(View.VISIBLE);
                holder.rl_addlayout.setOnClickListener(mOnClickListener);
            } else {
                holder.rl_addlayout.setVisibility(View.GONE);
            }
        } else {
            holder.rl_addlayout.setVisibility(View.GONE);
            holder.rl_boxlayout.setVisibility(View.VISIBLE);
            boxBean = boxBeanList.get(position);
            holder.listview_boxlist_boxtitle.setText(boxBean.getTitle());
            holder.listview_edit.setTag(boxBean);
            holder.listview_connect.setTag(boxBean);
            holder.rl_boxlayout.setTag(boxBean);
            holder.rl_boxlayout.setOnClickListener(mOnClickListener);
            holder.listview_edit.setOnClickListener(mOnClickListener);
            holder.listview_connect.setOnClickListener(mOnClickListener);
        }
        /**
         *Box列表前的颜色条设置
         */
        if (position % 3 == 0) {
            holder.listview_boxlist_linecolor.setBackgroundColor(context.getResources().getColor(R.color.boxlist_linecolor_blue));
        } else if (position % 3 == 1) {
            holder.listview_boxlist_linecolor.setBackgroundColor(context.getResources().getColor(R.color.boxlist_linecolor_yellow));
        } else {
            holder.listview_boxlist_linecolor.setBackgroundColor(context.getResources().getColor(R.color.boxlist_linecolor_red));
        }
        return convertView;
    }

    public void setOnItemClickListener(View.OnClickListener mOnTextOnClickListener) {
        mOnClickListener = mOnTextOnClickListener;
    }

    /**
     * 进入修改模式
     * @param change
     */
    public void startModify(boolean change) {
        this.isModify = change;
        notifyDataSetChanged();
    }

    /**
     * UI更改
     */
    @Override
    public void notifyDataSetChanged() {
        this.boxBeanList = SystemConstant.boxBeanList;
        super.notifyDataSetChanged();
    }

    static class ViewHolder {
        View listview_boxlist_linecolor;
        TextView listview_boxlist_boxtitle;
        ImageView listview_boxlist_arrow;
        RelativeLayout rl_boxlayout;
        RelativeLayout rl_addlayout;
        TextView listview_edit;
        TextView listview_connect;
    }
}
